package com.kgc.lab3;

import java.util.Objects;

public class Student extends Object {
    private int id;
    private String name;
    private int age;

    public Student() {
    }

    public Student(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    //重写Object类equals()和hashCode()

    //重写比较规则

    @Override
    public int hashCode() {
        System.out.println("重写Object类hashCode()方法 ...");
        return Objects.hash(id,name);
    }

    @Override
    public boolean equals(Object obj) {
        System.out.println("重写Object类equals()方法 ...");
         if(this == obj) return true;
         if(!(obj instanceof Student)) return false;
         Student another = (Student)obj;
         if(this.getId() == another.getId() && this.getName().equals(another.getName())){
             return true;
         }
         return false;
    }

    @Override
    public String toString() {
        System.out.println("重写Object类toString()方法，打印对象各个属性值 >>");
        return "学生信息：学号：" + this.getId() + ",姓名：" + this.getName() + ",年龄：" + this.getAge();

    }

    /*@Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }*/


    /* @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return id == student.id &&
                age == student.age &&
                Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name, age);
    }*/
}
